'use strict';
var gulp = require('gulp'),
  concat = require('gulp-concat'),
  less = require('gulp-less'),
  minifyCss = require('gulp-minify-css'),
  minify = require('gulp-minify'),
  plumber = require('gulp-plumber'),
  rename = require('gulp-rename'),
  zip = require('gulp-zip'),
  moment= require("moment"),
  sftp = require('gulp-sftp'),
  ftp = require('gulp-ftp'),
  git = require('gulp-git'),
  runSequence = require('run-sequence'),

// 载入外挂
//sass = require('gulp-ruby-sass'),
//autoprefixer = require('gulp-autoprefixer'),
  uglify = require('gulp-uglify'),
  imagemin = require('gulp-imagemin'),
  clean = require('gulp-clean'),
  notify = require('gulp-notify'),
  cache = require('gulp-cache'),
  htmlmin = require('gulp-htmlmin'),
//livereload = require('gulp-livereload');
  ngAnnotate = require('gulp-ng-annotate');





gulp.paths = {
  src: 'src',
  dist: 'dist',
  tmp: '.tmp',
  e2e: 'e2e'
};


var jsfiles =["bower_components/jquery/dist/jquery.js",
  "bower_components/angular/angular.js",
  "bower_components/angular-animate/angular-animate.js",
  "bower_components/angular-cookies/angular-cookies.js",
  "bower_components/angular-touch/angular-touch.js",
  "bower_components/angular-sanitize/angular-sanitize.js",
  "bower_components/angular-resource/angular-resource.js",
  "bower_components/bootstrap/dist/js/bootstrap.js",
  "bower_components/angular-bootstrap/ui-bootstrap-tpls.js",
  "bower_components/PACE/pace.js",
  "bower_components/metisMenu/dist/metisMenu.js",
  "bower_components/angular-translate/angular-translate.js",
  "bower_components/ng-idle/angular-idle.js",
  "bower_components/oclazyload/dist/ocLazyLoad.min.js",
  "bower_components/malarkey/dist/malarkey.min.js",
  "bower_components/AngularJS-Toaster/toaster.js",
  "bower_components/angular-bootstrap-multiselect/angular-bootstrap-multiselect.js",
  "bower_components/angular-ui-router/release/angular-ui-router.js",
  "bower_components/summernote/dist/summernote.js",
  "bower_components/angular-summernote/dist/angular-summernote.js",


  "app/app.js",
  "app/user/user_register.js",
  "app/user/userList.js",
  "app/user/setting.js",
  "app/user/register.js",
  "app/user/login.js",
  "app/user/hospitalCardCtrl.js",
  "app/user/forgotPassword.js",
  "app/validate/validate_print.js",
  "app/validate/validate.js",
  "app/validate/checkoutForm.js",
  "app/treatment/treatment.js",
  "app/treatment/treatRecord.js",
  "app/timeline/timelineCtrl.js",
  "app/templateJS/calendarCtrl.js",
  "app/record/schedule.js",
  "app/record/radiotherapyRecord.js",
  "app/record/afterloadingRecord.js",
  "app/makingModel/modeling.js",
  "app/makingModel/modelForm.js",
  "app/makingModel/makingModel.js",
  "app/makingModel/makingConfirm.js",
  "app/main/openForm.js",
  "app/main/main.controller.js",
  "app/doctor/doctorhome.js",
  "app/dashboard/director.js",
  "app/dashboard/RegistUser.js",
  "app/config/values.js",
  "app/config/users.js",
  "app/config/setting.js",
  "app/config/role.js",
  "app/config/modelList.js",
  "app/config/imageValues.js",
  "app/charging/payMoneyDetail.js",
  "app/charging/payMoney.js",
  "app/charging/charging.js",
  "app/EBP/EBPlist.js",
  "app/EBP/EBP.js",
  "app/Contouring02/contourImageList.js",
  "app/Contouring02/ContouringController.js",
  "app/Contouring/ContouringController.js",
  "app/CTimage/imageView.js",
  "app/CTimage/imageList.js",
  "components/common/topnavbar.js",
  "components/common/menuController.js",
  "app/translations.js",
  "app/inspinia.js",
  "app/filter.js",
  "app/factory.js",
  "app/directives.js",
];


var cssfiles =["bower_components/metisMenu/dist/metisMenu.css",
  "bower_components/animate.css/animate.css",
  "bower_components/AngularJS-Toaster/toaster.css",
  "bower_components/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css",
  "bower_components/summernote/dist/summernote.css"
];


var config = {
  input:{
    zip:["dist/**/*"],
    less:["src/app/index.less","src/app/less/style.less","src/app/vendor.less"],

    javascript:jsfiles,
    cssfiles:cssfiles,
    //cornerstone:["js/cornerstone*.js",'jquery.hotkeys.min.js',"!js/cornerstone*.min.js"],
    scss:'src/styles/main.scss',
    images:['src/**/*.png','src/**/*.jpg'],
    html:"src/**/*.html",
    bower_components:["bower_components/**/*"],
    css_folders:["src/css/**/*"],
    cp_js:["src/js/**/*"]
  },
  dist:{
    basePath:'dist',
    css:"css",
    javascript:"js",
    images:'img',
    html:"html",
    bower_components:'bower_components',
    zipfolder:"dist_zip"
  }
}





//压缩HTML
gulp.task('html', function () {
  var options = {
    removeComments: true,
    collapseWhitespace: true,
    collapseBooleanAttributes: true,
    removeEmptyAttributes: true,
    removeScriptTypeAttributes: true,
    removeStyleLinkTypeAttributes: true,
    minifyJS: true,
    minifyCSS: true
  };
  gulp.src(config.input.html)
    .pipe(htmlmin(options))
    // .pipe(rename({ suffix: '.min' }))
    // .pipe(gulp.dest(config.dist.basePath +"/"+config.dist.html))
    .pipe(gulp.dest(config.dist.basePath))
});

//压缩JavaScript
gulp.task('js', function (done) {
  gulp.src(config.input.javascript)
    .pipe(ngAnnotate({single_quotes: true}))
    .pipe(uglify())
    .pipe(concat('index.min.js'))
    .pipe(gulp.dest(config.dist.basePath))

});





//拷贝bower_components
gulp.task('copy.bs', function (done) {
  gulp.src(config.input.bower_components)
    .pipe(gulp.dest(config.dist.basePath+"/"+config.dist.bower_components));
  gulp.src(config.input.images)
    .pipe(gulp.dest(config.dist.basePath));

  gulp.src(config.input.cp_js)
    .pipe(gulp.dest(config.dist.basePath+"/"+"js"));


  gulp.src(config.input.css_folders)
    .pipe(gulp.dest(config.dist.basePath+"/"+config.dist.css));


});


//拷贝bower_components
gulp.task('copy.img', function (done) {
  gulp.src(config.input.cp_js)
    .pipe(gulp.dest(config.dist.basePath+"/"+"js"));
});


//拷贝bower_components
gulp.task('copy.js', function (done) {
  gulp.src(config.input.cp_js)
    .pipe(gulp.dest(config.dist.basePath+"/"+"js"));
  gulp.src("src/configure/**/*")
    .pipe(gulp.dest(config.dist.basePath+"/"+"configure"));
});



//拷贝bower_components
gulp.task('copy.css', function (done) {

});

gulp.task('copy', function () {
  gulp.start('copy.bs');
  gulp.start('copy.img');
  gulp.start('copy.css');
  gulp.start('copy.js');

});






gulp.task('less', function () {
  gulp.src(config.input.less)
    .pipe(less())
    .pipe(minifyCss())
    .pipe(concat('index.min.css'))
    .pipe(gulp.dest(config.dist.basePath));

  gulp.src(config.input.cssfiles)
    .pipe(minifyCss())
    .pipe(concat('loading.min.css'))
    .pipe(gulp.dest(config.dist.basePath))
});




gulp.task('default', function () {

  gulp.start('html');
  gulp.start('js');
  gulp.start('less');
  gulp.start('copy');
});


/*
 * version:1.0
 * date:2016-7-26
 * auth:wu.yu
 * desc:压缩dist文件夹为 dist_***.zip包
 * usage:gulp zip
 *
 *
 * version:1.1
 * date:2016-7-28
 * auth:wu.yu
 * desc:增加命令行参数为-user
 * usage:gulp zip --user wuyu
 *
 * */
gulp.task('zip', function (userName) {
  var timeStamp= moment().format("YYYY-MM-D_HH-mm-ss_");

  var user = require('minimist')(process.argv.slice(2)).user;

  git.exec({args : 'log'}, function (err, stdout) {
    var gitVersion = stdout.substr(7,8);
    console.log("gitVersion:"+gitVersion);
    gulp.src(config.input.zip)
      .pipe(zip("dist_"+user+"_"+timeStamp+gitVersion+".zip"))
      .pipe(gulp.dest(config.dist.zipfolder));
    //if (err) throw err;
  });

});
//gulp zip --user XXXX

gulp.task('git', function (userName) {
  var timeStamp= moment().format("YYYY-MM-D_HH-mm-ss_");

  var user = require('minimist')(process.argv.slice(2)).user;

  git.exec({args : 'log'}, function (err, stdout) {
    var gitVersion = stdout.substr(7,8);
    console.log(gitVersion);
    if (err) throw err;
  });

});





gulp.task('ftp', function () {
  return gulp.src("dist_zip/*")
    .pipe(ftp({
      host: 'linkingmed-xp',
      port:21,
      //user: 'anonymous',
      //pass:null,
      remotePath:"idoctor/"
    }));
});


// Other actions that do not require a Vinyl
gulp.task('gitlog', function(){
  git.exec({args : 'log'}, function (err, stdout) {
    console.log("gitlog:"+stdout.substr(7,8));

    if (err) throw err;
  });
});
